<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qiushi:mall:productspu:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>

      <el-table-column label="名称" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.row.type !== '3'">
            <span>{{ scope.row.name }}</span>
          </div>
          <div v-else-if="scope.row.type === '3'">
            <router-link :to="'/qiushimall/productspufile?id=' + scope.row.id" class="link-type">
              <span>{{ scope.row.name }}</span>
              <div>(修改视频)</div>
            </router-link>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="图片" align="center" prop="image">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img :src="scope.row.image" style="width:300px"/>
            <img :src="scope.row.image" style="max-width: 50px;max-height: 50px" slot="reference">
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="商品类型" align="center" prop="type">
        <template slot-scope="scope">
          <p>{{type_dict[scope.row.type - 1]['label']}}</p>
        </template>
      </el-table-column>

      <el-table-column label="出售价格" align="center" prop="price"/>
      <el-table-column label="积分" align="center" prop="point"/>
      <el-table-column label="连续包月价格" align="center" prop="monthlyPrice"/>
      <el-table-column label="连续包季价格" align="center" prop="quarterlyPrice"/>
      <el-table-column label="连续包年价格" align="center" prop="annualPrice"/>
      <el-table-column label="返利积分" align="center" prop="rebatePoint" />
      <el-table-column label="上级返利积分" align="center" prop="inviterRebatePoint" />
      <el-table-column label="类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <p>{{getType(scope.row.deviceType)}}</p>
        </template>
      </el-table-column>

      <el-table-column label="商品类别" align="center" prop="categoryId">
        <template slot-scope="scope">
          <p>{{getCate(scope.row.categoryId)}}</p>
        </template>
      </el-table-column>

      <el-table-column label="销售数量" align="center" prop="sellCount"/>
      <el-table-column label="库存数量" align="center" prop="stockCount"/>
      <el-table-column label="购买须知" align="center" prop="buyNotes"/>
      <el-table-column label="商品详情" align="center" prop="detailedDesc"/>

      <el-table-column label="商品状态" align="center" prop="status">
        <template slot-scope="scope">
          <p>{{status_dict[scope.row.status - 1]['label']}}</p>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qiushi:mall:productspu:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qiushi:mall:productspu:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加商品对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>

        <el-form-item label="商品图片" prop="imageList">
          <image-upload
            v-model="form.imageList"
            limit="5"/>
        </el-form-item>

        <el-form-item label="商品类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in type_dict"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <template v-if="form.type === '1'">
          <el-form-item label="积分" prop="point">
            <el-input v-model="form.point" placeholder="请输入积分"/>
          </el-form-item>
        </template>

        <template v-if="form.type === '2'">
          <el-form-item label="出售价格" prop="price">
            <el-input v-model="form.price" placeholder="请输入出售价格"/>
          </el-form-item>
        </template>

        <template v-if="form.type === '3'">
          <el-form-item label="连续包月价格" prop="monthlyPrice">
            <el-input v-model="form.monthlyPrice" placeholder="请输入连续包月价格"/>
          </el-form-item>
          <el-form-item label="连续包季价格" prop="quarterlyPrice">
            <el-input v-model="form.quarterlyPrice" placeholder="请输入连续包季价格"/>
          </el-form-item>
          <el-form-item label="连续包年价格" prop="annualPrice">
            <el-input v-model="form.annualPrice" placeholder="请输入连续包年价格"/>
          </el-form-item>
          <el-form-item label="返利积分" prop="rebatePoint">
            <el-input v-model="form.rebatePoint" placeholder="请输入返利积分" />
          </el-form-item>
          <el-form-item label="上级返利积分" prop="inviterRebatePoint">
            <el-input v-model="form.inviterRebatePoint" placeholder="请输入上级返利积分" />
          </el-form-item>

          <el-form-item label="问卷" prop="type">
            <el-select v-model="form.formId" placeholder="请选择问题问卷">
              <el-option
                v-for="item in formList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="类型" prop="deviceType">
            <el-select v-model="form.deviceType" placeholder="请选择类型">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>

        <el-form-item label="商品类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择类型">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="库存数量" prop="stockCount">
          <el-input v-model="form.stockCount" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="购买须知" prop="buyNotes">
          <el-input v-model="form.buyNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品详情">
          <editor v-model="form.detailedDesc" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>

        <el-form-item label="商品图片" prop="imageList">
            <image-upload
              v-model="form.imageList"
              limit="5"/>
        </el-form-item>

        <template v-if="form.type === '1'">
          <el-form-item label="积分" prop="point">
            <el-input v-model="form.point" placeholder="请输入积分"/>
          </el-form-item>
        </template>

        <template v-if="form.type === '2'">
          <el-form-item label="出售价格" prop="price">
            <el-input v-model="form.price" placeholder="请输入出售价格"/>
          </el-form-item>
        </template>

        <template v-if="form.type === '3'">
          <el-form-item label="连续包月价格" prop="monthlyPrice">
            <el-input v-model="form.monthlyPrice" placeholder="请输入连续包月价格"/>
          </el-form-item>
          <el-form-item label="连续包季价格" prop="quarterlyPrice">
            <el-input v-model="form.quarterlyPrice" placeholder="请输入连续包季价格"/>
          </el-form-item>
          <el-form-item label="连续包年价格" prop="annualPrice">
            <el-input v-model="form.annualPrice" placeholder="请输入连续包年价格"/>
          </el-form-item>
          <el-form-item label="返利积分" prop="rebatePoint">
            <el-input v-model="form.rebatePoint" placeholder="请输入返利积分" />
          </el-form-item>
          <el-form-item label="上级返利积分" prop="inviterRebatePoint">
            <el-input v-model="form.inviterRebatePoint" placeholder="请输入上级返利积分" />
          </el-form-item>

          <el-form-item label="问卷" prop="type">
            <el-select v-model="form.formId" placeholder="请选择问题问卷">
              <el-option
                v-for="item in formList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="类型" prop="deviceType">
            <el-select v-model="form.deviceType" placeholder="请选择类型">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>

        <el-form-item label="商品类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择类型">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="库存数量" prop="stockCount">
          <el-input v-model="form.stockCount" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="购买须知" prop="buyNotes">
          <el-input v-model="form.buyNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品详情">
          <editor v-model="form.detailedDesc" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addSpu, delSpu, getSpu, listCate, listType, listSpu, updateSpu} from "@/api/qiushi/mall/productspu";
import {getToken} from "@/utils/auth";
import {listForm} from "@/api/qiushi/followup/qiushiform";

export default {
  name: "Productspu",
  data() {
    return {
      // 文件上传
      actionUrl: process.env.VUE_APP_BASE_API + "/common/upload",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // useless
      cateList: [],
      typeList: [],
      formList: [],
      // 字典
      type_dict: [{
        value: '1',
        label: '积分商品'
      },{
        value: '2',
        label: '普通商品'
      },{
        value: '3',
        label: '服务商品'
      },],
      status_dict: [{
        value: '1',
        label: '上架'
      },{
        value: '2',
        label: '下架'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      spuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        image: null,
        price: null,
        point: null,
        monthlyPrice: null,
        quarterlyPrice: null,
        annualPrice: null,
        deviceType: null,
        freight: null,
        sellCount: null,
        stockCount: null,
        isRecommend: null,
        categoryId: null,
        categoryName: null,
        buyNotes: null,
        detailedDesc: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceType: [
          {required: true, message: "类型不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "出售价格不能为空", trigger: "blur"}
        ],
        point: [
          {required: true, message: "积分不能为空", trigger: "blur"}
        ],
        monthlyPrice: [
          {required: true, message: "连续包月价格不能为空", trigger: "blur"}
        ],
        quarterlyPrice: [
          {required: true, message: "连续包季价格不能为空", trigger: "blur"}
        ],
        annualPrice: [
          {required: true, message: "连续包年价格不能为空", trigger: "blur"}
        ],
        freight: [
          {required: true, message: "运费不能为空", trigger: "blur"}
        ],
        sellCount: [
          {required: true, message: "销售数量不能为空", trigger: "blur"}
        ],
        stockCount: [
          {required: true, message: "库存数量不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "商品类型  1:积分商品 2:普通商品 3:服务商品不能为空", trigger: "change"}
        ],
        status: [
          {required: true, message: "1上架 2下架  不能为空", trigger: "change"}
        ],
        delFlag: [
          {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 文件上传 */
    // useless
    getCate(categoryId) {
      for (let i = 0; i < this.cateList.length; i++) {
        if (this.cateList[i].id === categoryId) return this.cateList[i].name
      }
    },
    getType(deviceType) {
      for (let i = 0; i < this.typeList.length; i++) {
        if (this.typeList[i].id === deviceType) return this.typeList[i].name
      }
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;

      // useless
      listCate().then(response => {
        this.cateList = response.rows;
      })

      listForm({}).then(response => {
        this.formList = response.rows;
      });

      listType().then(response => {
        this.typeList = response.rows;
      })

      listSpu(this.queryParams).then(response => {
        this.spuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        image: null,
        price: null,
        point: null,
        monthlyPrice: null,
        quarterlyPrice: null,
        annualPrice: null,
        deviceType: null,
        formId: null,
        freight: null,
        sellCount: null,
        stockCount: null,
        isRecommend: null,
        rebatePoint: null,
        inviterRebatePoint: null,
        categoryId: null,
        categoryName: null,
        buyNotes: null,
        detailedDesc: null,
        type: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,

        imageList: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpu(id).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log("图片",this.form.imageList);

      // return
      this.$refs["form"].validate(valid => {
        if (valid) {
          /** 图片上传 */
          console.log("imageList",this.form)
          // if (this.form.imageList != null) {
          //   let tempList = this.form.imageList.split(',')
          //   this.form.imageList = ''
          //   for (let i = 0; i < tempList.length; i++) {
          //     if (tempList[i].startsWith("/profile/upload")) {
          //       this.form.imageList += process.env.VUE_APP_BASE_API + tempList[i] + ','
          //     }
          //   }
          // }

          if (this.form.id != null) {
            updateSpu(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            addSpu(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function () {
        return delSpu(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/mall/productspu/export', {
        ...this.queryParams
      }, `spu_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
